//
//  LzwAdressSearchVC.m
//  NewHalfsugar
//
//  Created by 刘志武 on 16/1/12.
//  Copyright © 2016年 刘志武. All rights reserved.
//

#import "LzwAdressSearchVC.h"
#import "LzwbaseTableView.h"
#import "LzwBaseLable.h"
#import "LzwBaseView.h"
#import "Block.h"
#import "LzwAdressSeachCell.h"
#import "LzwAdressSearchEndVC.h"

@interface LzwAdressSearchVC ()<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate>

@property (nonatomic, retain) NSMutableArray *searchAllArray;
@property (nonatomic, retain) NSMutableArray *arrayToCell;

@property (nonatomic, retain) LzwbaseTableView *mySearchTableView;

@property(nonatomic,retain)UISearchBar *mySearchBar;

@property(nonatomic,retain)UISearchController *mySearchController;


@end

@implementation LzwAdressSearchVC

-(void)dealloc{
    [_mySearchBar release];
    [_mySearchTableView release];
    [_mySearchController release];
    [_searchAllArray release];
    [_arrayToCell release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"搜宝贝";
    
    self.searchAllArray = [NSMutableArray array];
    
    
    UIImage *LandRightBar = [UIImage imageNamed:@"return.png"];
    LandRightBar = [LandRightBar imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:LandRightBar style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    
    self.mySearchController = [[UISearchController alloc]initWithSearchResultsController:nil];
//    self.mySearchController.searchResultsUpdater = self;
    self.mySearchController.searchBar.delegate = self;
    //    设置开始搜索时背景显示与否 页面背景阴影效果,一般不要设置.
    self.mySearchController.dimsBackgroundDuringPresentation = YES;
    //设置点击搜索框时候隐藏导航栏
    self.mySearchController.hidesNavigationBarDuringPresentation = NO;
    self.mySearchController.searchBar.frame = CGRectMake(self.mySearchController.searchBar.frame.origin.x, self.mySearchController.searchBar.frame.origin.y, self.mySearchController.searchBar.frame.size.width, 44.0 * WIDTH);
    //    [searchBar sizeToFit]:设置searchBar位置自适应
    [self.mySearchController.searchBar sizeToFit];
    
    self.mySearchTableView = [[LzwbaseTableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.mySearchTableView];
    [_mySearchTableView release];
    
    self.mySearchTableView.delegate = self;
    self.mySearchTableView.dataSource = self;
    
    self.mySearchTableView.tableHeaderView = self.mySearchController.searchBar;
    
   
    
    
    [self.mySearchTableView registerClass:[LzwAdressSeachCell class] forCellReuseIdentifier:@"cell"];
    self.mySearchTableView.separatorStyle = NO;
    
    self.arrayToCell = [NSMutableArray array];

    
    NSString *string = @"http://open3.bantangapp.com/base/search/hottags";
    
    NSDictionary *dic = @{@"app_installtime":@"1451365479.933367", @"app_versions":@"5.1.1", @"channel_name":@"appStore", @"client_id":@"bt_app_ios", @"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5", @"device_token":@"c02c60075e7bab110fbdf27e15de3cb61e97360c4ea3545fd8a5936e46c28ccb", @"oauth_token":@"a41d3848f43102aa7759c27ba2c0fe8f", @"os_versions":@"9.1", @"screensize":@"640", @"track_device_info":@"iPhone5%2C3", @"track_deviceid":@"A80585E3-D6AD-4A53-BE1C-5963901C96E5", @"track_user_id":@"1746436", @"v":@"9"};
    
    [Block creatingDataTask:string parameters:dic Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        
       NSMutableArray *array = [resuposeObject objectForKey:@"data"];
        
        for (NSString *str in array) {
        
          
            
            [self.arrayToCell addObject:str];
            
            
        }
        
        [self.mySearchTableView reloadData];
        
    }];
    
    

}

//row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrayToCell.count;
    
}
//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwAdressSeachCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    cell.backgroundColor = [UIColor colorWithRed:70/ 255.0 green:67 / 255.0 blue:30 / 255.0 alpha:0.5];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.str = [self.arrayToCell objectAtIndex:indexPath.row];
    
    return cell;
}
//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}

//设置section头部
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375 * WIDTH, 50 * WIDTH)];


    LzwBaseLable *lable = [[LzwBaseLable alloc]initWithFrame:CGRectMake((375 - 150) / 2 * WIDTH, 10 * WIDTH, 150 * WIDTH, 30 * WIDTH)];
    lable.text = @"热门搜索";
    lable.textColor = [UIColor lightGrayColor];
        lable.textAlignment = NSTextAlignmentCenter;
    [view addSubview:lable];
    
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
    
}
//点击收索按钮就会执行这个方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
    
    NSString *searchString = [self.mySearchController.searchBar text];

    NSString *str = @"http://open3.bantangapp.com/base/search/list";
//
    NSDictionary *dic = @{@"app_installtime":@"1451365479.933367", @"app_versions":@"5.1.1", @"channel_name":@"appStore", @"client_id":@"bt_app_ios", @"client_secret":@"9c1e6634ce1c5098e056628cd66a17a5", @"device_token":@"c02c60075e7bab110fbdf27e15de3cb61e97360c4ea3545fd8a5936e46c28ccb", @"keyword":searchString, @"oauth_token":@"a41d3848f43102aa7759c27ba2c0fe8f", @"os_versions":@"9.1", @"screensize":@"640", @"track_device_info":@"iPhone5%2C3", @"track_deviceid":@"A80585E3-D6AD-4A53-BE1C-5963901C96E5", @"track_user_id":@"1746436", @"type_id":@"2", @"v":@"9"};
    

    [self.searchAllArray removeAllObjects];
//
    /**
     *  由于url的一个参数可能为汉字,我们需要用下面的方法转换一下字符串,才能被AFN使用
     */
//    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    

    [Block creatingDataTask:str parameters:dic Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {

        
        NSMutableArray *arrayA = [resuposeObject objectForKey:@"data"];
        for (NSMutableDictionary *dic in arrayA) {
            
            LzwAdressSeachEndModle *modle = [[LzwAdressSeachEndModle alloc]init];
            
            [modle setValuesForKeysWithDictionary:dic];
            
            [self.searchAllArray addObject:modle];
            
            [modle release];
            
        }
        
        
    }];
    


    
        LzwAdressSearchEndVC *seachEndVc = [[LzwAdressSearchEndVC alloc]init];
        
        UINavigationController *naVc = [[UINavigationController alloc]initWithRootViewController:seachEndVc];
        
        [self presentViewController:naVc animated:YES completion:^{
            
            seachEndVc.arrayA = self.searchAllArray;
            seachEndVc.navigationItem.title = searchString;
            
            
        }];
    

}

     

//UISearchController的移除
//在viewWillDisappear中要将UISearchController移除, 否则切换到下一个View中, 搜索框仍然会有短暂的存在.
- (void)viewWillDisappear:(BOOL)animated {
         [super viewWillDisappear:animated];
         if (self.mySearchController.active) {
             self.mySearchController.active = NO;
             [self.mySearchController.searchBar removeFromSuperview];
         }
     }
     
- (void)leftButtonAction:(UIBarButtonItem *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"dsadsadsfdfsfdsf");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
